/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.clientserver;

/**
 *
 * @author Mr.Sam
 */
import fr.ove.utils.Connection;
import fr.ove.utils.ConnectionHandler;
import fr.ove.openmath.mathematica.ResponseReader;
import java.io.IOException;

/**
* A ConnectionHandler implementation.<BR>
* This connection handler is used with a server which connects to an another one.
* The objects sent from a connected client are sent to the server the server whose
* instance deals the conections is connected.
*/
public class RepeatConnectionHandler implements ConnectionHandler, Runnable {    
    /**
    * The connection with the client connected to the server.
    */
    private Connection clientConnection;
    
    /**
    * The reader from the connection with the client connected to the server.
    */
    private ResponseReader reader = new ResponseReader();
    
    /**
    * The connection with the server the server that the instance handles the connection
    * is connected.
    */
    private Connection serverConnection;
    
    
    /**
    * The constructor.
    * @param hostName the name of the host the instance connects to.
    * @param portNumber the port number through the instance is connected.
    */
    public RepeatConnectionHandler(String hostName, int portNumber) {
        serverConnection = new Connection(hostName, portNumber);
        try {
            serverConnection.open();
            // Connection to server ok. Start the thread.
            new Thread(this).start();
        }
        catch (IOException ioe) {
            System.out.println("Failed to open a connection to the server");
            ioe.printStackTrace();
        }
    }
    
    // #################################
    // ### Interfaces Implementation ###
    // #################################    

    // ConnectionHandler interface

    /**
    * Handles the specified connection.
    * @param connection the connection to handle.
    */
    public void handleConnection(Connection connection) {
        // just keep a refence of the connection
        clientConnection = connection;
    }
    

    // Runnable interface

    /**
    *
    */
    public void run() {
        String object = null;
        
        while (true) {
            
            try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}
            
            System.out.println("on bloucle  et clientConnection = " + clientConnection);
            
            if (clientConnection != null) {
                System.out.println("On attend un obj");
                // Read the object sent by the client.
                object = reader.read(clientConnection.getInputStream());
                // if we got it, send it to the server
                if (object != null) {
                    try {
                        System.out.println("on a lu obj ====> envoie au serveur");
                        serverConnection.getOutputStream().write(object.getBytes());
                        // Wait for the response from the server
                        System.out.println("on attends reponse server");
                        object = reader.read(serverConnection.getInputStream());
                        // if we got something, send it to the client
                        if (object != null) {
                            try {
                                System.out.println("on envoie reponse au client");
                                clientConnection.getOutputStream().write(object.getBytes());
                            }
                            catch (IOException ioe) {
                                System.out.println("Failed to send back response to the client");
                                ioe.printStackTrace();
                            }
                        }
                    }
                    catch (IOException ioe) {
                        System.out.println("Failed to piped the object to the server");
                        ioe.printStackTrace();
                    }
                }
            }
        }
    }
}